package it.nesimuge.score.blue;

import it.lapomuge.db.entity.Partecipazione;
import it.nesimuge.score.gui.listener.LinstenerPartecipazione;

import java.util.ArrayList;
import java.util.List;

import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;

public class BluetoothServer extends Thread {

    public final UUID uuid = new UUID( // the uid of the service, it has to be
            // unique,
            "0000110100001000800000805F9B34FB", false); // it can be generated
    // randomly
    public final String name = "Echo Server"; // the name of the service
    public final String url = "btspp://localhost:" + uuid // the service url
            + ";name=" + name + ";authenticate=false;encrypt=false;";
    LocalDevice local = null;
    StreamConnectionNotifier server = null;
    private LinstenerPartecipazione listener;
    private Partecipazione partecipazione;
    StatoGiudice stato = new StatoGiudice();
    List<ThreadGuidice> list = new ArrayList<ThreadGuidice>();

    public BluetoothServer() {
    }

    @Override
    public void run() {
        try {
            System.out.println("Setting device to be discoverable...");
            local = LocalDevice.getLocalDevice();
            local.setDiscoverable(DiscoveryAgent.GIAC);
            System.out.println("Start advertising service...");
            server = (StreamConnectionNotifier) Connector.open(url);
            System.out.println("Waiting for incoming connection...");
            ServiceRecord record = LocalDevice.getLocalDevice().getRecord(
                    server);
            System.out.println(record.getConnectionURL(0, false));

            // DataInputStream din = new
            // DataInputStream(conn.openInputStream());

            int i = 0;
            while (true) {
                StreamConnection conn = server.acceptAndOpen();
                System.out.println("Is open connection");

                synchronized (Partecipazione.class) {
                    ThreadGuidice t = new ThreadGuidice(conn);
                    t.setName("Guidice " + i);
                    t.setPartecipazioneListener(listener);
                    t.setPartecipazione(partecipazione);
                    t.setStato(stato);
                    t.start();
                    this.list.add(t);
                }



            }

        } catch (Exception e) {
            System.out.println("Exception Occured: " + e.toString());
            e.printStackTrace();
        }
    }

    public void stopThread() {
        this.stop();
    }

    public void setParticepazioneListener(LinstenerPartecipazione d) {
        for (ThreadGuidice t : list) {
            t.setPartecipazioneListener(d);
        }
        this.listener = d;
    }

    public synchronized void setParticepazione(Partecipazione p) {
        synchronized (Partecipazione.class) {

            System.out.println("Partecipazione Server:" + p);
            this.partecipazione = p;
            for (ThreadGuidice t : list) {
                t.setPartecipazione(partecipazione);
            }
        }
    }
}
